package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.mainFrameToolBar;

import javax.swing.JButton;
import javax.swing.JToolBar;

import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.common.UserCommandListener;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table.FlightLogTableController;

public class MainFrameToolBarController {

	private static MainFrameToolBarController INSTANCE;

	private JToolBar mainFrameToolbar;

	private JButton addFlightButton;

	private JButton editFlightButton;

	private JButton deleteFlightButton;

	private MainFrameToolBarController() {
	}

	/**
	 * Singleton
	 * 
	 * @return Singleton
	 */
	public static MainFrameToolBarController instance() {

		if (INSTANCE == null) {

			INSTANCE = new MainFrameToolBarController();
		}
		return INSTANCE;
	}

	public JToolBar getMainFrameToolbar() {
		

		if (mainFrameToolbar == null) {

			mainFrameToolbar = new JToolBar();
			mainFrameToolbar.add(getAddFlightButton());
			mainFrameToolbar.add(getEditFlightButton());
			mainFrameToolbar.add(getDeleteFlightButton());
		}
		return mainFrameToolbar;
	}

	private JButton getAddFlightButton() {

		if (addFlightButton == null) {

			addFlightButton = new JButton("+");

			addFlightButton.setToolTipText("Add Flight");

			addFlightButton.addActionListener(UserCommandListener.instance());

			addFlightButton
					.setActionCommand(UserCommandListener.ADD_FLIGHT_RECORD);
		}
		return addFlightButton;
	}

	private JButton getEditFlightButton() {

		if (editFlightButton == null) {

			editFlightButton = new JButton("E");

			editFlightButton.addActionListener(UserCommandListener.instance());

			editFlightButton
					.setActionCommand(UserCommandListener.EDIT_FLIGHT_RECORD);
		}
		return editFlightButton;
	}

	private JButton getDeleteFlightButton() {

		if (deleteFlightButton == null) {
			deleteFlightButton = new JButton("D");
			deleteFlightButton
					.addActionListener(UserCommandListener.instance());
			deleteFlightButton
					.setActionCommand(UserCommandListener.DELETE_FLIGHT_RECORD);
		}
		return deleteFlightButton;
	}

	/**
	 * Configures items based on whether or not there is a flight log rec
	 * selected in the main display table
	 */
	public void configureItemsRequiringTableSelection() {

		boolean bTableHasSelection = FlightLogTableController.instance()
				.isRowSelected();

		getEditFlightButton().setEnabled(bTableHasSelection);
		getDeleteFlightButton().setEnabled(bTableHasSelection);

	}

}
